package cn.edu.scut.hsrc.util;

import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.io.Writer;
import java.util.List;

import cn.edu.scut.hsrc.bean.Doc;
import cn.edu.scut.hsrc.output.ClusterResult;

/**
 * 输出工具类
 * @file Output.java
 * @author feiyu
 *
 */

public class OutputUtil {
	
	/**
	 * 把聚类后的结果输出到文件
	 * @param query 查询词
	 * @param list	聚类后的结果链表
	 * @param path	待保存的文件路径
	 */
	public static void print(String query,List<ClusterResult> list,String path) {
		File file = new File(path);
		file.getParentFile().mkdirs();
		try {
			file.createNewFile();
			BufferedWriter fileWriter = new BufferedWriter(new FileWriter(file));
			print(query, list, fileWriter);
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	
	/**
	 * 把聚类后的结果输出到文件
	 * @param query		查询词
	 * @param results	聚类后的结果数组
	 * @param path		待保存的文件路径
	 */
	public static void print(String query,ClusterResult[] results,String path) {
		File file = new File(path);
		file.getParentFile().mkdirs();
		try {
			file.createNewFile();
			BufferedWriter fileWriter = new BufferedWriter(new FileWriter(file));
			print(query, results, fileWriter);
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	
	/**
	 * 把聚类结果输出到对应的流里面去
	 * @param query	  	   查询词
	 * @param results	   聚类结果
	 * @param writer 	  流
	 */
	public static void print(String query,ClusterResult [] results,Writer writer) {
		try {
			writer.write("查询词:" + query + "\n");
			int size = results.length;
			//size = Math.min(size, 10);
			for (int i = 0; i < size; i++) {
				ClusterResult tempResult = results[i];
				writer.write("第" + (i+1) + "个类：\n");
				writer.write(tempResult.getLabel().getText()+"\n");
				List<Doc> docs = tempResult.getDocs();
				int num = docs.size();
				for (int j = 0; j < num; j++) {
					writer.write("第" + j + "个：\n");
					Doc doc = docs.get(j);
					writer.write("title："+doc.getTitle()+"\n");
					writer.write("summary："+doc.getSummary()+"\n");
					writer.write("docIndex： "+doc.getIndex()+"\n");
				}
				writer.write("\n\n");
			}
			writer.flush();
			//writer.close();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	
	
	/**
	 * 把聚类结果输出到对应的流里面去
	 * @param query	   查询词
	 * @param list	   聚类结果
	 * @param writer 流
	 */
	public static void print(String query,List<ClusterResult> list,Writer writer) {
		try {
			writer.write("查询词:" + query + "\n");
			int size = list.size();
			//size = Math.min(size, 10);
			for (int i = 0; i < size; i++) {
				ClusterResult tempResult = list.get(i);
				writer.write("第" + (i+1) + "个类：\n");
				writer.write(tempResult.getLabel().getText()+"\n");
				List<Doc> docs = tempResult.getDocs();
				int num = docs.size();
				for (int j = 0; j < num; j++) {
					writer.write("第" + j + "个：\n");
					Doc doc = docs.get(j);
					writer.write("title："+doc.getTitle()+"\n");
					writer.write("summary："+doc.getSummary()+"\n");
					writer.write("docIndex： "+doc.getIndex()+"\n");
				}
				writer.write("\n\n");
			}
			writer.flush();
			//writer.close();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	
	
	/**
	 * 打印标签
	 * @param query
	 * @param list
	 * @param writer
	 */
	public static void printLabel(String query,List<ClusterResult> list,Writer writer) {
		try {
			writer.write("查询词:" + query + "\n");
			if(list == null)
				return;
			int size = list.size();
			//size = Math.min(size, 10);
			for (int i = 0; i < size; i++) {
				ClusterResult tempResult = list.get(i);
				writer.write("第" + (i+1) + "个类：");
				writer.write(tempResult.getLabel().getText()+"\n");
			}
			writer.flush();
			//writer.close();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	
	/**
	 * 打印标签
	 * @param query
	 * @param clusterResults
	 * @param writer
	 */
	public static void printLabel(String query,ClusterResult [] clusterResults,Writer writer) {
		try {
			if(clusterResults == null)
				return;
			writer.write("查询词:" + query + "\n");
			int size = clusterResults.length;
			//size = Math.min(size, 10);
			for (int i = 0; i < size; i++) {
				ClusterResult tempResult = clusterResults[i];
				writer.write("第" + (i+1) + "个类：");
				writer.write(tempResult.getLabel().getText()+"("+tempResult.getDocs().size()+")\n");
			}
			writer.flush();
			//writer.close();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	
	
	/**
	 * 输出文档
	 * @param query
	 * @param docs
	 * @param writer
	 */
	public static void print(String query,Doc[] docs,Writer writer) {
		try {
			writer.write("查询词:" + query + "\n");
			int size = docs.length;
			for (int i = 0; i < size; i++) {
				writer.write(docs[i].toString()+"\n");
			}
			writer.flush();
			//writer.close();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	
	/**
	 * 输出文档
	 * @param query
	 * @param docs
	 * @param path
	 */
	public static void print(String query,Doc[] docs,String path) {
		File file = new File(path);
		file.getParentFile().mkdirs();
		try {
			file.createNewFile();
			BufferedWriter fileWriter = new BufferedWriter(new FileWriter(file));
			print(query, docs, fileWriter);
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
}
